<template>
  <!-- 新增弹窗 -->
  <div>
    <el-dialog
      :title="formData.id?'编辑房产':'添加房产'"
      :visible="showDialog"
      @close="closeFn"
    >
      <!-- 表单与选择器 -->
      <el-form ref="form" :model="formData" :rules="rules">
        <!-- 分类 -->
        <el-form-item label="分类" label-width="50px" prop="category">
          <el-select v-model="formData.category" placeholder="请选择分类">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- 经纪 -->
        <el-form-item label="经纪" label-width="50px" prop="agent">
          <el-select v-model="formData.agent" placeholder="请选择经纪">
            <el-option
              v-for="item in agentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- 标题 -->
        <el-form-item label="标题" label-width="50px" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <!-- 标签 -->
        <el-form-item label="标签" label-width="50px" prop="tags">
          <el-tag
            v-for="item in formData.tags"
            :key="item"
            closable
            :disable-transitions="false"
            @close="handleClose(item)"
          >
            <!-- handleClose删除标签 -->
            {{ item }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="tag"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          />
          <!-- handleInputConfirm 确认新增标签-->
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+新增标签</el-button>
          <!-- showInput新增标签 -->
        </el-form-item>
        <!-- 地址 -->
        <el-form-item label="地址" prop="address" label-width="50px">
          <el-cascader
            ref="cascader"
            v-model="selectedOptions"
            size="large"
            :options="addressOptions"
            clearable
            placeholder="请选择地址"
            @change="handleChange"
          />
        </el-form-item>
        <!-- 价格 -->
        <el-form-item label="价格" label-width="50px" prop="price">
          <el-input v-model="formData.price" placeholder="请输入价格" />
        </el-form-item>
        <!-- 描述 -->
        <el-form-item label="描述" label-width="50px" prop="desc">
          <el-input
            v-model="formData.desc"
            type="text"
            placeholder="请输入描述"
          />
        </el-form-item>
        <!-- 内容 -->
        <el-form-item label="内容" label-width="50px" prop="content">
          <el-input
            v-model="formData.content"
            type="textarea"
            :rows="3"
            placeholder="请输入内容"
          />
        </el-form-item>
        <!-- 介绍图片 -->
        <el-form-item label="介绍图片">
          <UploadImage ref="introduceImg" />
        </el-form-item>
        <!-- 封面 -->
        <el-form-item label="封面">
          <UploadImage ref="coverImg" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeFn">取 消</el-button>
        <el-button type="primary" size="small" @click="addFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAgentsList, getCateGoriesList, addHouseList, getHouseById, deitHouseList } from '@/api/house'
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
import { Message } from 'element-ui'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 添加内容表单数据
      formData: {
        title: '', // 标题
        desc: '', // 房产描述
        image: '', // 封面的图片
        agent: '', // 经纪人
        content: '', // 前台显示的内容
        address: '', // 房产地址
        price: '', // 房产价格
        photos: [],
        tags: [], // 标签的数组tags
        category: '' // 分类
      },
      tag: '', // 标签默认为空
      categoryList: [], // 分类列表
      agentList: [], // 经纪人列表，
      inputVisible: false, // 默认不可见
      inputValue: '', // 输入框绑定的内容默认为空
      addressOptions: regionData, // 渲染的地址的数组通过regionData来获取输入的地址
      selectedOptions: [], // 绑定的地址的数组
      rules: {
        category: [
          { required: true, message: '请选择分页', trigger: 'blur' }
        ],
        agent: [
          { required: true, message: '请选择经纪', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请选择地址', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' }
        ],
        tags: [
          { required: true, message: '请输入标签', trigger: 'blur' }
        ]
      }
    }
  },
  async  created() {
    //  获取分类列表
    const res = await getCateGoriesList()
    // console.log('125', res)
    this.categoryList = res

    // 获取经纪列表
    const agent = await getAgentsList()
    // console.log('130', agent)
    this.agentList = agent
  },
  methods: {
    // 编辑数据按钮
    async getHouseListById(id) {
      const res = await getHouseById(id)
      // console.log('162', res)
      this.formData = {
        ...res,
        category: {
          title: res.category.title,
          id: res.category.id
        }
      }
      const arr = res.address.split(',')
      this.selectedOptions = TextToCode[arr[0]][arr[1]][arr[2]].code
      this.$nextTick(() => {
        this.$refs.coverImg.percent = 100
        this.$refs.introduceImg.percent = 100
        this.$refs.coverImg.imgUrl = res.image
        this.$refs.introduceImg.imgUrl = res.photos
      })
    },
    // 新增数据按钮
    async addFn() {
      // 通过表单效验才添加内容
      this.$refs.form.validate()
      const introduceImg = this.$refs.introduceImg
      console.log('163', introduceImg)
      const coverImg = this.$refs.coverImg
      console.log('165', coverImg)
      // percent是上传的进度
      if (coverImg.imgUrl !== 0) {
        if (coverImg.percent !== 100) {
          return this.$message.warning('封面还没有上传完成，请稍后')
        }
        //  封面判断
        this.formData.image = coverImg.imgUrl
      } else {
        return this.$message.warning('请上传封面图片')
      }
      if (introduceImg.imgUrl !== 0) {
        if (introduceImg.percent !== 100) {
          return this.$message.warning('介绍照片还没有上传完成，请稍后')
        }
        // 介绍判断
        this.formData.photos = introduceImg.imgUrl
      } else {
        return this.$message.warning('至少上传1张介绍图片')
      }
      // 判断是新增还是编辑
      if (!this.formData.id) {
        // 新增请求
        await addHouseList(this.formData)
        Message.success('新增成功')
      } else {
        // 编辑请求
        await deitHouseList(this.formData)
        Message.success('编辑成功')
      }
      // 通知父组件刷新列表
      this.$emit('addEvent')
      // 关闭弹窗
      this.closeFn()
    },
    // 取消按钮
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空表单数据
      this.formData = {
        title: '', // 标题
        desc: '', // 房产描述
        image: '', // 封面的图片
        agent: '', // 经纪人
        content: '', // 前台显示的内容
        address: '', // 房产地址
        price: '', // 房产价格
        photos: [],
        tags: [], // 标签的数组tags
        category: '' // 分类
      }
      // 清空地址绑定的地址信息
      this.selectedOptions = []
      // 清除图片
      this.$refs.coverImg.imgUrl = ''
      this.$refs.introduceImg.imgUrl = ''
      // 取消效验
      this.$refs.form.resetFields()
    },
    // 删除标签
    handleClose(item) {
      this.formData.tags.splice(this.formData.tags.indexOf(item), 1)
    },
    //  新增标签
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 确认新增标签
    handleInputConfirm() {
      const inputValue = this.tag
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.tag = ''
      this.inputVisible = false
    },
    // 地区选择
    handleChange(value) {
      console.log('185', value)
      // 因为澳门和香港只有两个选项
      if (value[0] === '810000' || value[0] === '820000') {
        this.formData.address = CodeToText[value[0]] + ',' + CodeToText[value[1]]
      } else {
        this.formData.address = CodeToText[value[0]] + ',' + CodeToText[value[1]] + ',' + CodeToText[value[2]]
      }
    }

  }

}
</script>

<style lang="scss" scoped>

</style>
